import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';

/*
  Generated class for the HomeService provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class HttpService {

  constructor(public http: Http) {
    console.log('Hello HttpService Provider');
  }

  get(URL:string) {
    console.log(URL);
    return new Promise((resolve, reject) => {
      this.http.get(URL)
        .map(res => res.json())
        .subscribe(data => {
          resolve(data);
        }, err => {
          reject(err);
        })
    })
  }

  //这种post方式在php中后台无法获取到数据，我们需要采用另外一种方式
  post(URL:string,data?:any) {
    let header = new Headers();
    header.append('Content-Type', 'application/x-www-form-urlencoded');
    let pramas = JSON.stringify(data)
    return new Promise((resolve, reject) => {
      this.http.post(URL, pramas, header)
        .map(res => res.json())
        .subscribe(data => resolve(data), err => reject(err))
    })
  }

         
}